package com.gzyj.video.config;


import com.gzyj.pojo.gps.GzyjVideoHistory;
import com.gzyj.video.mapper.GzyjVideoHistoryMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ftpserver.ftplet.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MyFtpPlet extends DefaultFtplet {
    private static Logger logger = LoggerFactory.getLogger(MyFtpPlet.class);
    @Value("${ftp.server.savePath}")
    private String savePath;

  /*  public MyFtpPlet(String savePath){
        this.savePath=savePath;
    }*/

    @Override
    public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
        if (request.getCommand().equalsIgnoreCase("cwd")) {
            File file = new File(savePath + request.getArgument());
            if (!file.exists()) {
                file.mkdirs();
            }
        }

        return super.beforeCommand(session, request);
    }

    @Override
    public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException {
        return super.afterCommand(session, request, reply);
    }

    @Override
    public FtpletResult onMkdirStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
      /*  if(request.getCommand().equalsIgnoreCase("mkd")){
            File file=new File(savePath+request.getArgument());
            if(!file.exists()){
                file.mkdirs();
            }
        }*/


        return super.onMkdirStart(session, request);
    }

    @Override
    public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {

        return super.onMkdirEnd(session, request);
    }

    @Override
    public FtpletResult onAppendStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onAppendStart(session, request);
    }

    @Autowired
    private GzyjVideoHistoryMapper gzyjVideoHistoryMapper;

    @Override
    public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        String fileName = request.getArgument();
        if (StringUtils.isNotEmpty(fileName)) {
            FtpFile file = null;
            try {
                file = session.getFileSystemView().getFile(fileName);
            } catch (Exception var32) {
                var32.printStackTrace();
            }
            fileName = file.getAbsolutePath();

            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");

            String[] param = fileName.split("/");
            List<String> p = Arrays.stream(param).filter(e -> StringUtils.isNotEmpty(e)).collect(Collectors.toList());
            String simNo = p.get(0);
            String cmd = p.get(1);
            String channelNo = p.get(2);
            String startTime = p.get(3);
            String endTime = p.get(4);
            String fileN=p.get(5);
            GzyjVideoHistory videoHistory=new GzyjVideoHistory();
            videoHistory.setSimNo(simNo);
            videoHistory.setLogicChannelNo(Integer.valueOf(channelNo));
            try {
                videoHistory.setBeginTime(sdf.parse(startTime));
                videoHistory.setEndTime(sdf.parse(endTime));
                videoHistory.setStoragePath(fileName);
                gzyjVideoHistoryMapper.insertOrUpdate(videoHistory);
            }catch (Exception e){
                e.printStackTrace();
            }


            logger.info("车辆:'{}'，文件名称为：'{}'，状态：完成上传~", simNo, fileN);
        }


        return super.onAppendEnd(session, request);
    }

    @Override
    public FtpletResult onUploadUniqueStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onUploadUniqueStart(session, request);
    }

    @Override
    public FtpletResult onUploadUniqueEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        return super.onUploadUniqueEnd(session, request);
    }

    @Override
    public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
            throws FtpException, IOException {
        //获取上传文件的上传路径
        String path = session.getUser().getHomeDirectory();

        //获取上传用户
        String name = session.getUser().getName();
        //获取上传文件名
        String filename = request.getArgument();
        logger.info("用户:'{}'，上传文件到目录：'{}'，文件名称为：'{}'，状态：开始上传~", name, path, filename);
        return super.onUploadStart(session, request);
    }


    @Override
    public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
            throws FtpException, IOException {
        //获取上传文件的上传路径
        String path = session.getUser().getHomeDirectory();
        //获取上传用户
        String name = session.getUser().getName();
        //获取上传文件名
        String filename = request.getArgument();
        logger.info("用户:'{}'，上传文件到目录：'{}'，文件名称为：'{}，状态：成功！'", name, path, filename);
        return super.onUploadEnd(session, request);
    }

    @Override
    public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
        //todo servies...
        return super.onDownloadStart(session, request);
    }

    @Override
    public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
        //todo servies...
        return super.onDownloadEnd(session, request);
    }

}
